﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Guide
{
    public partial class ClusterControl : System.Web.UI.UserControl
    {

        private List<Province> _listProvince;

        public List<Province> ListProvince
        {
            get { return _listProvince; }
            set { _listProvince = value; }
        }

        private string _clusterName;

        public string ClusterName
        {
            get { return _clusterName; }
            set { _clusterName = value; }
        }

        private int _everyPageCount;

        public int EveryPageCount
        {
            get { return _everyPageCount; }
            set { _everyPageCount = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            int i = 0;
            int count = ListProvince.Count;
            List<Province> listOrderBy = _listProvince.OrderBy(o => o.ProvinceCName).ToList();

            int remainder = count - i * EveryPageCount;
            while (remainder > 0)
            {
                var control = (EveryPage)LoadControl("EveryPage.ascx");
                control.ID = _clusterName + "_EveryPage_" + i;
                if (remainder >= EveryPageCount)
                {
                    control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, (i + 1) * EveryPageCount);

                }
                else
                {
                    control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, remainder);
                }
                this.Controls.Add(control);
                //catch (ArgumentException a)
                //{

                //    if (remainder > 0)
                //    {

                //    }
                //    this.Controls.Add(control);
                //    break;
                //}
                //i++;
            }

            //while (true)
            //{
            //    var control = (EveryPage)LoadControl("EveryPage.ascx");
            //    control.ID = _clusterName + "_EveryPage_" + i;
            //    try
            //    {
            //        control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, (i + 1) * EveryPageCount);
            //        this.Controls.Add(control);
            //    }
            //    catch (ArgumentException a)
            //    {
            //        int remainder = count - i * EveryPageCount - 1;
            //        if (remainder > 0)
            //        {
            //            control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, remainder);
            //        }
            //        this.Controls.Add(control);
            //        break;
            //    }
            //    i++;
            //}

            //foreach ()
            //{
            //    var control = (Stock)LoadControl("Every.ascx");
            //    control.ID = "Hello" + i;
            //    control.LabelContext = item;
            //    control.ListProvince = ((List<Province>)Session["listprovince"]).Where(o => o.ClusterID == item).ToList();
            //    this.Controls.Add(control);
            //    i = i + 1;
            //}
        }
    }
}